package com.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin/role")
public class AdminRoleController {

    @Autowired
    private AdminRoleService adminRoleService;

    @RequestMapping("/adminRole")
    public ResponseBean adminRole(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10",name = "limit") int size, String text){
        IPage iPage =  adminRoleService.findRoleByAdmin(page,size,text);
//        System.out.println(iPage.getRecords());
        return new ResponseBean(0, iPage.getTotal(),iPage.getRecords());
    }

    @RequestMapping("/adminRole_edit")
    public ResponseBean adminRoleEdit(String adminId){
        Map map =  adminRoleService.findAllRole(adminId);
        return new ResponseBean(0, "",map);
    }

    @RequestMapping("/adminRole_save")
    public ResponseBean adminRoleSave(String adminId, String roleId){
//        System.out.println(adminId);
        Map map = adminRoleService.adminRoleSave(adminId,roleId);
        if((int) map.get("n")>0){
            return new ResponseBean(200,"赋予角色成功",roleId);
        }
        return new ResponseBean(500,"赋予角色失败",null);
    }

    @RequestMapping("/adminRole_del")
    public ResponseBean adminRoleDel(String adminId, String adminRoleId){
        int n = adminRoleService.adminRoleDel(adminId,adminRoleId);
        if(n>0){
            return new ResponseBean(200,"解除角色成功",null);
        }
        return new ResponseBean(500,"解除角色失败",null);
    }
}
